package com.shouhuobao.station.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.iseastar.guojiang.BaseActivity;
import com.iseastar.guojiang.logic.AppHttp;
import com.shouhuobao.take.utils.StringUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import droid.frame.utils.android.Alert;

/**
 * 微信分享回调接口
 *
 * @author coffee<br>
 *         2015-10-21下午8:46:50
 */
public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            //确认登录成功
            case BaseResp.ErrCode.ERR_OK:
                if (StringUtils.isEmpty(resp.openId)) {
                    Alert.toast("授权成功");
                    SendAuth.Resp sendResp = (SendAuth.Resp) resp;
                    if (sendResp != null) {
                        String code = sendResp.code;
                        AppHttp.getInstance().getAccess_token(code);
                        // 关闭微信分享的对话框
//				HandlerMgr.sendMessage(MsgID.weixin_share_close, code);
                    }
                }else {
                    Alert.toast("分享成功");
                }
                break;
            //用户取消操作
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Alert.toast("取消操作");
                break;
            //用户拒绝操作
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                break;
            default:
                Alert.toast("系统异常" + resp.errCode);
                break;
        }
        finish();
    }

}
